package com.k8sops.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.k8sops.backend.common.ApiResponse;
import com.k8sops.backend.model.entity.DOSRelease;
import com.k8sops.backend.model.vo.ReleaseVO;
import com.k8sops.backend.service.DOSReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Release控制器
 */
@RestController
@RequestMapping("/release")
@Api(tags = "Release管理")
public class DOSReleaseController {

    @Resource
    private DOSReleaseService dosReleaseService;

    /**
     * 分页查询Release列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询Release列表")
    public ApiResponse<IPage<DOSRelease>> pageReleases(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("项目ID") @RequestParam(required = false) Integer projectId,
            @ApiParam("模块ID") @RequestParam(required = false) Integer moduleId,
            @ApiParam("环境类型") @RequestParam(required = false) Integer envType,
            @ApiParam("集群ID") @RequestParam(required = false) Integer clusterId,
            @ApiParam("命名空间ID") @RequestParam(required = false) Integer namespaceId) {
        
        IPage<DOSRelease> result = dosReleaseService.pageReleases(
                new Page<>(page, size), projectId, moduleId, envType, clusterId, namespaceId);
        return ApiResponse.success(result);
    }
    
    /**
     * 分页查询Release列表，并关联项目和项目组信息
     */
    @GetMapping("/page/with-project-info")
    @ApiOperation(value = "分页查询Release列表，并关联项目和项目组信息")
    public ApiResponse<IPage<DOSRelease>> pageReleasesWithProjectInfo(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("项目ID") @RequestParam(required = false) Integer projectId,
            @ApiParam("模块ID") @RequestParam(required = false) Integer moduleId,
            @ApiParam("环境类型") @RequestParam(required = false) Integer envType,
            @ApiParam("集群ID") @RequestParam(required = false) Integer clusterId,
            @ApiParam("命名空间") @RequestParam(required = false) String namespace,
            @ApiParam("Release名称") @RequestParam(required = false) String releaseName,
            @ApiParam("项目组ID") @RequestParam(required = false) Integer projectGroupId) {
        
        IPage<DOSRelease> result = dosReleaseService.pageReleasesWithProjectInfo(
                new Page<>(page, size), projectId, moduleId, envType, clusterId, namespace, releaseName, projectGroupId);
        return ApiResponse.success(result);
    }

    /**
     * 获取Release详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取Release详情")
    public ApiResponse<DOSRelease> getReleaseById(@PathVariable Long id) {
        DOSRelease release = dosReleaseService.getById(id);
        return release != null ? ApiResponse.success(release) : ApiResponse.error("Release不存在");
    }

    /**
     * 删除Release
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除Release")
    public ApiResponse<Boolean> deleteRelease(@PathVariable Long id) {
        boolean result = dosReleaseService.removeById(id);
        return result ? ApiResponse.success(true) : ApiResponse.error("删除Release失败");
    }
}
